package com.cx.mall.pay.config;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfig {

    @Value("${http.client.connect-timeout:30000}")
    private int connectTimeout;

    @Value("${http.client.read-timeout:60000}")
    private int readTimeout;

    @Value("${http.client.max-connections:100}")
    private int maxConnections;

    @Value("${http.client.max-connections-per-route:20}")
    private int maxConnectionsPerRoute;

    @Bean
    public CloseableHttpClient httpClient() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(maxConnections);
        connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);

        return HttpClients.custom()
                .setConnectionManager(connectionManager)
                .build();
    }
}

